package com.heima.user.service.impl;

import com.heima.user.mapper.UserMapper;
import com.heima.user.service.UserService;
import com.heima.common.exception.CustomException;
import com.heima.model.user.dtos.UserDto;
import com.heima.model.user.pojos.User;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 查询列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(UserDto dto) {
        //1 参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 分页检查
        dto.checkParam();

        //2 执行查询
        // 分页
        Page page = new Page(dto.getPage(), dto.getSize());
        // 条件查询
        LambdaQueryWrapper<User> wraaper = new LambdaQueryWrapper<>();

        // 用户名称
         if (StringUtils.isNotBlank(dto.getUsername())) {
            wraaper.like(User::getUsername, dto.getUsername());
         }
        // 密码
         if (StringUtils.isNotBlank(dto.getPassword())) {
            wraaper.like(User::getPassword, dto.getPassword());
         }
        // 昵称
         if (StringUtils.isNotBlank(dto.getNickname())) {
            wraaper.like(User::getNickname, dto.getNickname());
         }
        // 头像地址
         if (StringUtils.isNotBlank(dto.getHeadImgUrl())) {
            wraaper.like(User::getHeadImgUrl, dto.getHeadImgUrl());
         }
        // 电话
         if (StringUtils.isNotBlank(dto.getPhone())) {
            wraaper.like(User::getPhone, dto.getPhone());
         }
        // 性别
         if (StringUtils.isNotBlank(dto.getSex())) {
            wraaper.like(User::getSex, dto.getSex());
         }
        // 状态
         if (StringUtils.isNotBlank(dto.getStatus())) {
            wraaper.like(User::getStatus, dto.getStatus());
         }
        // 城市编码
         if (StringUtils.isNotBlank(dto.getCityCode())) {
            wraaper.like(User::getCityCode, dto.getCityCode());
         }
        // 来源
         if (StringUtils.isNotBlank(dto.getSource())) {
            wraaper.like(User::getSource, dto.getSource());
         }
        // 微信openid
         if (StringUtils.isNotBlank(dto.getOpenId())) {
            wraaper.like(User::getOpenId, dto.getOpenId());
         }
        // 关联id
         if (StringUtils.isNotBlank(dto.getUnionId())) {
            wraaper.like(User::getUnionId, dto.getUnionId());
         }
        // app
         if (StringUtils.isNotBlank(dto.getApp())) {
            wraaper.like(User::getApp, dto.getApp());
         }



        IPage pageResult = page(page, wraaper);

        //3 封装分页结果
        return PageResponseResult.page(dto.getPage(), dto.getSize(),
                (int) pageResult.getTotal(),pageResult.getRecords());
    }

    /**
     * 新增
     * @param user
     * @return
     */
    @Override
    public ResponseResult insert(User user) {
        //1 参数检查
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 执行保存
        save(user);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(User user) {

        //1.检查参数
        if(null == user){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        updateById(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Long id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
